﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using MovieDB.Common;
using MovieDB.Controllers.Common;
using MovieDB.DTOs;
using MovieDB.Models;
using MovieDB.ServiceInterfaces;

namespace MovieDB.Controllers
{
    public class MovieController : MovieBaseControler
    {
        private readonly IMovieService _movieService;
        private readonly ISearchService _searchService;

        public MovieController(IMovieService movieService, ISearchService searchService)
        {
            _movieService = movieService;
            _searchService = searchService;
        }

        [HttpGet]
        public ActionResult Add()
        {
            var movieModel = ModelFactory.CreateMovieModel(ModelCRUDType.Create);

            return View(movieModel);
        }

        [HttpPost]
        public ActionResult Add(MovieModel movie)
        {
            if (ModelState.IsValid)
            {
                var movieDto = Mapping.MapFromSourceToDestination<MovieModel, MovieDto>(movie);
                _movieService.SaveMovie(movieDto);

                TempData["Info"] = "Saved successfully.";
                TempData["Post"] = "HttpPost";
            }
            return View(movie);
        }

        [HttpGet]
        public ActionResult Edit(Guid id)
        {
            MovieDto movieDto = _movieService.GetMovieById(id);
            var movie = Mapping.MapFromSourceToDestination<MovieDto, MovieModel>(movieDto);

            return View(movie);
        }

        [HttpPost]
        public ActionResult Edit(MovieModel movie)
        {
            if (ModelState.IsValid)
            {
                var movieDto = Mapping.MapFromSourceToDestination<MovieModel, MovieDto>(movie);
                _movieService.SaveMovie(movieDto);

                TempData["Info"] = "Saved successfully.";
            }
            return View(movie);
        }

        [HttpGet]
        public ActionResult Delete(Guid id)
        {
            _movieService.DeleteMovie(id);
            TempData["Info"] = "Delete successfully.";
           
            return RedirectToAction("Index", "Home");
        }

        [HttpGet]
        public JsonResult IsTitleAvailable(string title)
        {
            Guid? movieId = GetMovieIdFromQuerystring(HttpContext.Request.UrlReferrer.Segments);
            bool isTitleAvailabel = _searchService.IsTitleAvailable(title, movieId);

            return Json(isTitleAvailabel, JsonRequestBehavior.AllowGet);
        }

        private Guid? GetMovieIdFromQuerystring(IEnumerable<string> segments)
        {
            if (segments == null || !segments.Any())
            {
                throw new ArgumentNullException("segments");
            }

            Guid movieId;

            var splitedMovieId = segments.Last();

            if (splitedMovieId.Length < 2)
            {
                return null;
            }

            if (!Guid.TryParse(splitedMovieId, out movieId))
            {
                return null;
            }

            return movieId;
        }
    }
}